﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MySql.Data.MySqlClient;
using Fotball_01.nifs;
using Fotball_01.DB;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;

namespace Fotball_01
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //string input1 = "<a class='nifs_link' href=\"head2head.php?land=14&amp;t=109&amp;u=6132&amp;lag1=991&amp;lag2=1026\">Dinamo Tbilisi <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f23.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(GEO)</font> - Flora Tallinn <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f31.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(EST)</font></a>";
            //string input2 = "<a class='nifs_link' href=\"head2head.php?land=1&amp;t=18&amp;u=5678&amp;lag1=168&amp;lag2=1154\">Sprint-Jeløy - Moss 2</a>";
            //string input3 = "<a class='nifs_link' href=\"head2head.php?land=14&amp;t=109&amp;u=6132&amp;lag1=1994&amp;lag2=8963\">Llanelli <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f32.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(WAL)</font> - Tauras Taurag&#279; <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f27.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(LTU)</font></a>";
            //string input4 = "<a class='nifs_link' href=\"head2head.php?land=14&amp;t=109&amp;u=6132&amp;lag1=955&amp;lag2=745\">Kalmar <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f5.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(SWE)</font> - EB/Streymur <img height=\"11\" style=\"border: none\" src=\"http://www.ba.no/nifs-static/gfx/flagg/11/f28.gif\" alt=\"flagg\"> <font style=\"font-weight:normal;color:black;\">(FRO)</font></a>";

            //string pattern = @"<a class='nifs_link' href=.head2head\.php\?land=.*&amp;t=.*&amp;u=.*&amp;lag1=.*&amp;lag2=.*>(?<lag1>[\w\s&#;/]+)<img height=.11.*- (?<lag2>[\w\s&#;/]+)<img height=.11.*</a>";
            //Match result = Regex.Match(input1, pattern);
            
            Kamp testKamp = new Kamp("RBK", "VIF", 2, 1, "SPILT");
            Thread testThread = new Thread(new ThreadStart((void)testKamp.ToString));

        }

        Database db = new Database();
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            List<List<string>> tabell = db.Query("DESCRIBE statistics");

            textBlock1.Text = "";

            for (int i = 0; i < tabell.Count; i++)
            {
                for (int j = 0; j < tabell[i].Count; j++)
                {
                    textBlock1.Text += tabell[i][j] + "\t";
                }

                textBlock1.Text += "\n";
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // Legg datoer i stringer fra datepicker
            string dateFrom = datePickerFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
            string dateTo = datePickerTo.SelectedDate.Value.ToString("yyyy-MM-dd");

            // Legg datoene inn i DateTime
            string[] dateArrFrom = dateFrom.Split('-');
            DateTime dateTimeFrom = new DateTime(Int32.Parse(dateArrFrom[0]), Int32.Parse(dateArrFrom[1]), Int32.Parse(dateArrFrom[2]));
            string[] dateArrTo = dateTo.Split('-');
            DateTime dateTimeTo = new DateTime(Int32.Parse(dateArrTo[0]), Int32.Parse(dateArrTo[1]), Int32.Parse(dateArrTo[2]));
            
            textBlock1.Text = "";

            if (dateTimeFrom.CompareTo(dateTimeTo) > 0)
                textBlock1.Text = "Fra-dato må være før eller samme som Til-dato";
            else
            {
                Nifs nifs = new Nifs();
                int pages = 0;
                // Start ved dateFrom
                DateTime jump = new DateTime(Int32.Parse(dateArrFrom[0]), Int32.Parse(dateArrFrom[1]), Int32.Parse(dateArrFrom[2]));
                while (true)
                {
                    // Skriv dagens dato
                    textBlock1.Text += "\t\t" + jump.ToString("yyyy-MM-dd") + "\n\n";

                    // Hent dagens kamper
                    List<Kamp> kamper = nifs.KamperIdag(jump.ToString("yyyy-MM-dd"));

                    // Skriv dagens kamper
                    for (int i = 0; i < kamper.Count; i++)
                    {
                        textBlock1.Text += kamper[i].Lag1 + " - " + kamper[i].Lag2 + "   (" + kamper[i].MaalLag1 + " - " + kamper[i].MaalLag2 + ")     ###!%%&( " + kamper[i].Status + "\n";
                    }

                    // Legg til mellomrom for ny dag
                    textBlock1.Text += "\n\n";

                    // Øk antall sider som er lastet
                    pages++;
                    pagesLoaded.Text = pages.ToString();

                    // Hopp ut hvis vi har kommet til toDate.
                    if (jump.Equals(dateTimeTo))
                        break;

                    // Hopp dagevis fremover inntil treff på dateTimeTo
                    jump = jump.AddDays(1);

                }
            }
        }
    }
}
